package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.application.query.vo.HiddenDangerVo;
import com.smedi.ismedi.inmail.core.domain.dto.HiddenDangerQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.HiddenDangerRemoteApi;
import com.smedi.ismedi.safetycheck.dto.request.danger.HiddenDangerListRequest;
import com.smedi.ismedi.safetycheck.dto.response.danger.HiddenDangerResponse;
import com.smedi.ismedi.safetycheck.feign.RemoteHiddenDangerFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class HiddenDangerRemoteApiImpl implements HiddenDangerRemoteApi {
    private final RemoteHiddenDangerFeignApi remoteHiddenDangerFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<HiddenDangerVo> getHiddenDangerList(HiddenDangerQuery hiddenDangerQuery) {
        HiddenDangerListRequest hiddenDangerListRequest = adapterConverter.hiddenDangerQueryToHiddenDangerListRequest(hiddenDangerQuery);
        ResponseData<List<HiddenDangerResponse>> responseData = remoteHiddenDangerFeignApi.getHiddenDangerList(hiddenDangerListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.hiddenDangerResponseToHiddenDanger(responseData.getData());
    }
}
